home *** CD-ROM | disk | FTP | other *** search
/ The 640 MEG Shareware Studio 2 / The 640 Meg Shareware Studio CD-ROM Volume II (Data Express)(1993).ISO / clang / c_course.zip / CHAP03.TXT < prev    next >
Text File  |  1989-12-30  |  18KB  |  389 lines

  1.                         Chapter 3 - Program Control                        
  2.  
  3.  
  4.                                THE WHILE LOOP
  5.  
  6.              The  C programming language has several structures  for 
  7.         looping  and conditional branching.   We will cover them all 
  8.         in this chapter and we will begin with the while loop.   The 
  9.         while  loop continues to loop while some condition is  true.
  10.         When   the   condition  becomes  false,   the   looping   is 
  11.         discontinued.   It therefore does just what it says it does, 
  12.         the name of the loop being very descriptive. 
  13.  
  14.              Load the program WHILE.C and display it for an  example 
  15.         of  a while loop.   We begin with a comment and the  program 
  16.         name,  then  go  on  to define an integer  variable  "count" 
  17.         within the body of the program.  The variable is set to zero 
  18.         and we come to the while loop itself.  The syntax of a while 
  19.         loop is just as shown here.  The keyword "while" is followed 
  20.         by an expression of something in parentheses,  followed by a 
  21.         compound  statement  bracketed by braces.   As long  as  the 
  22.         expression in parenthesis is true, all statements within the 
  23.         braces will be executed.   In this case,  since the variable 
  24.         count  is incremented by one every time the  statements  are 
  25.         executed, it will eventually reach 6, the statement will not 
  26.         be executed,  and the loop will be terminated.   The program 
  27.         control   will   resume  at  the  statement  following   the 
  28.         statements in braces. 
  29.  
  30.              We  will  cover  the compare  expression,  the  one  in 
  31.         parentheses, in the next chapter.  Until then, simply accept 
  32.         the  expressions for what you think they should do  and  you 
  33.         will probably be correct.
  34.  
  35.              Several  things must be pointed out regarding the while 
  36.         loop.   First,  if the variable count were initially set  to 
  37.         any  number greater than 5,  the statements within the  loop 
  38.         would  not be executed at all,  so it is possible to have  a 
  39.         while  loop  that  never  is  executed.   Secondly,  if  the 
  40.         variable  were  not incremented in the loop,  then  in  this 
  41.         case,  the loop would never terminate, and the program would 
  42.         never complete.   Finally, if there is only one statement to 
  43.         be executed within the loop, it does not need braces but can 
  44.         stand alone.
  45.  
  46.              Compile and run this program.
  47.  
  48.                              THE DO-WHILE LOOP
  49.  
  50.              A  variation  of the while loop is illustrated  in  the 
  51.         program DOWHILE.C,  which you should load and display.  This 
  52.         program  is nearly identical to the last one except that the 
  53.         loop  begins  with the reserved word  "do",  followed  by  a 
  54.         compound  statement  in  braces,   then  the  reserved  word 
  55.  
  56.  
  57.                                  Page 12 
  58.  
  59.  
  60.  
  61.  
  62.  
  63.  
  64.  
  65.  
  66.  
  67.                         Chapter 3 - Program Control                        
  68.  
  69.  
  70.         "while",  and  finally  an expression in  parentheses.   The 
  71.         statements in the braces are executed repeatedly as long  as 
  72.         the expression in parentheses is true.   When the expression 
  73.         in parentheses becomes false,  execution is terminated,  and 
  74.         control passes to the statements following this statement.
  75.  
  76.              Several  things  must  be pointed  out  regarding  this 
  77.         statement.  Since  the test is done at the end of the  loop, 
  78.         the  statements  in  the braces will always be  executed  at 
  79.         least once.   Secondly,  if "i" were not changed within  the 
  80.         loop,  the loop would never terminate, and hence the program 
  81.         would  never terminate.   Finally,  just like for the  while 
  82.         loop,  if  only  one statement will be executed  within  the 
  83.         loop,  no braces are required.  Compile and run this program 
  84.         to see if it does what you think it should do.
  85.  
  86.              It  should come as no surprise to you that these  loops 
  87.         can be nested.  That is, one loop can be included within the 
  88.         compound  statement of another loop,  and the nesting  level 
  89.         has no limit.
  90.  
  91.                                 THE FOR LOOP
  92.  
  93.              The  "for" loop is really nothing new,  it is simply  a 
  94.         new  way  to describe the "while" loop.   Load and edit  the 
  95.         file  named  FORLOOP.C for an example of a  program  with  a 
  96.         "for"  loop.  The  "for" loop consists of the reserved  word 
  97.         "for" followed by a rather large expression in  parentheses.  
  98.         This expression is really composed of three fields separated 
  99.         by  semi-colons.   The  first field contains the  expression 
  100.         "index  = 0" and is an initializing field.   Any expressions 
  101.         in  this field are executed prior to the first pass  through 
  102.         the loop.   There is essentially no limit as to what can  go 
  103.         here,  but  good programming practice would require it to be 
  104.         kept simple.   Several initializing statements can be placed 
  105.         in this field, separated by commas.
  106.  
  107.              The second field,  in this case containing "index < 6", 
  108.         is  the  test which is done at the beginning  of  each  loop 
  109.         through  the program.   It can be any expression which  will 
  110.         evaluate  to a true or false.   (More will be said about the 
  111.         actual value of true and false in the next chapter.)
  112.  
  113.              The expression contained in the third field is executed 
  114.         each time the loop is executed but it is not executed  until 
  115.         after  those  statements  in the main body of the  loop  are 
  116.         executed.   This field, like the first, can also be composed 
  117.         of several operations separated by commas.
  118.  
  119.              Following  the  for()  expression  is  any  single   or 
  120.         compound statement which will be executed as the body of the 
  121.  
  122.  
  123.                                  Page 13 
  124.  
  125.  
  126.  
  127.  
  128.  
  129.  
  130.  
  131.  
  132.  
  133.                         Chapter 3 - Program Control                        
  134.  
  135.  
  136.         loop.   A  compound  statement  is  any  group  of  valid  C 
  137.         statements enclosed in braces.   In nearly any context in C, 
  138.         a  simple statement can be replaced by a compound  statement 
  139.         that will be treated as if it were a single statement as far 
  140.         as program control goes.  Compile and run this program.
  141.          
  142.                               THE IF STATEMENT
  143.  
  144.              Load  and  display the file IFELSE.C for an example  of 
  145.         our first conditional branching statement, the "if".  Notice 
  146.         first,  that there is a "for" loop with a compound statement 
  147.         as its executable part containing two "if" statements.  This 
  148.         is an example of how statements can be nested.  It should be 
  149.         clear  to  you  that each of the  "if"  statements  will  be 
  150.         executed 10 times.
  151.  
  152.              Consider the first "if" statement.   It starts with the 
  153.         keyword  "if" followed by an expression in parentheses.   If 
  154.         the expression is evaluated and found to be true, the single 
  155.         statement following the "if" is executed,  and if false, the 
  156.         following  statement  is  skipped.   Here  too,  the  single 
  157.         statement  can be replaced by a compound statement  composed 
  158.         of  several statements bounded by  braces.   The  expression 
  159.         "data  == 2" is simply asking if the value of data is  equal 
  160.         to 2,  this will be explained in detail in the next chapter.  
  161.         (Simply suffice for now that if "data = 2" were used in this 
  162.         context, it would mean a completely different thing.)
  163.  
  164.                             NOW FOR THE IF-ELSE
  165.  
  166.              The  second  "if"  is  similar to the  first  with  the 
  167.         addition  of a new reserved word,  the "else" following  the 
  168.         first  printf  statement.   This  simply says  that  if  the 
  169.         expression in the parentheses evaluates as true,  the  first 
  170.         expression  is executed,  otherwise the expression following 
  171.         the "else" is executed.   Thus,  one of the two  expressions 
  172.         will  always be executed,  whereas in the first example  the 
  173.         single expression was either executed or skipped.  Both will 
  174.         find  many uses in your C programming efforts.   Compile and 
  175.         run this program to see if it does what you expect.
  176.  
  177.                            THE BREAK AND CONTINUE
  178.  
  179.              Load  the file named BREAKCON.C for an example  of  two 
  180.         new statements.  Notice that in the first "for", there is an 
  181.         if  statement that calls a break if xx equals 8.   The break 
  182.         will  jump  out of the loop you are in and  begin  executing 
  183.         statements following the loop,  effectively terminating  the 
  184.         loop.   This  is a valuable statement when you need to  jump 
  185.         out  of  a  loop  depending on the  value  of  some  results 
  186.         calculated  in the loop.   In this case,  when xx reaches 8, 
  187.  
  188.  
  189.                                  Page 14 
  190.  
  191.  
  192.  
  193.  
  194.  
  195.  
  196.  
  197.  
  198.  
  199.                         Chapter 3 - Program Control                        
  200.  
  201.  
  202.         the  loop is terminated and the last value printed  will  be 
  203.         the previous value, namely 7.
  204.  
  205.              The  next  "for" loop,  contains a  continue  statement 
  206.         which  does not cause termination of the loop but jumps  out 
  207.         of the present iteration.  When the value of xx reaches 8 in 
  208.         this case,  the program will jump to the end of the loop and 
  209.         continue  executing  the loop,  effectively eliminating  the 
  210.         printf statement during the pass through the loop when xx is 
  211.         eight.   Compile and run the program to see if it does  what 
  212.         you expect.
  213.  
  214.                             THE SWITCH STATEMENT
  215.  
  216.              Load  and  display the file SWITCH.C for an example  of 
  217.         the  biggest construct yet in the C  language,  the  switch.  
  218.         The switch is not difficult, so don't let it intimidate you.  
  219.         It  begins with the keyword "switch" followed by a  variable 
  220.         in parentheses which is the switching variable, in this case 
  221.         "truck".   As many cases as desired are then enclosed within 
  222.         a pair of braces.  The reserved word "case" is used to begin 
  223.         each  case  entered followed by the value of  the  variable, 
  224.         then a colon, and the statements to be executed.
  225.  
  226.              In  this example,  if the variable "truck" contains the 
  227.         value 3 during this pass of the switch statement, the printf 
  228.         will  cause "The value is three" to be  displayed,  and  the 
  229.         "break" statement will cause us to jump out of the switch. 
  230.  
  231.              Once  an  entry  point is  found,  statements  will  be 
  232.         executed until a "break" is found or until the program drops 
  233.         through  the bottom of the switch braces.   If the  variable 
  234.         has  the value 5,  the statements will begin executing where 
  235.         "case  5  :" is found,  but the first statements  found  are 
  236.         where the case 8 statements are.  These are executed and the 
  237.         break  statement  in the "case 8" portion  will  direct  the 
  238.         execution  out the bottom of the switch.   The various  case 
  239.         values can be in any order and if a value is not found,  the 
  240.         default portion of the switch will be executed.
  241.  
  242.              It should be clear that any of the above constructs can 
  243.         be  nested  within  each  other  or  placed  in  succession, 
  244.         depending on the needs of the particular programming project 
  245.         at hand.
  246.  
  247.              Compile  and  run SWITCH.C to see if it does  what  you 
  248.         expect it to after this discussion.
  249.  
  250.              Load  and display the file GOTOEX.C for an example of a 
  251.         file  with some "goto" statements in it.   To use  a  "goto" 
  252.         statement,  you simply use the reserved word "goto" followed 
  253.  
  254.  
  255.                                  Page 15 
  256.  
  257.  
  258.  
  259.  
  260.  
  261.  
  262.  
  263.  
  264.  
  265.                         Chapter 3 - Program Control                        
  266.  
  267.  
  268.         by the symbolic name to which you wish to jump.  The name is 
  269.         then  placed  anywhere in the program followed by  a  colon.  
  270.         You  are  not  allowed to jump into any loop,  but  you  are 
  271.         allowed to jump out of a loop.  Also, you are not allowed to 
  272.         jump out of any function into another.   These attempts will 
  273.         be  flagged by your compiler as an error if you attempt  any 
  274.         of them.
  275.  
  276.              This  particular program is really a mess but it  is  a 
  277.         good example of why software writers are trying to eliminate 
  278.         the  use of the "goto" statement as much as  possible.   The 
  279.         only place in this program where it is reasonable to use the 
  280.         "goto"  is the one in line 17 where the program jumps out of 
  281.         the three nested loops in one jump.   In this case it  would 
  282.         be  rather messy to set up a variable and jump  successively 
  283.         out of all three loops but one "goto" statement gets you out 
  284.         of all three.
  285.  
  286.              Some  persons say the "goto" statement should never  be 
  287.         used  under  any  circumstances but this  is  rather  narrow 
  288.         minded  thinking.   If there is a place where a "goto"  will 
  289.         clearly  do a neater control flow than some other construct, 
  290.         feel free to use it.  It should not be abused however, as it 
  291.         is in the rest of the program on your monitor.
  292.  
  293.              Entire  books  are written on  "gotoless"  programming, 
  294.         better known as Structured Programming.   These will be left 
  295.         to  your  study.   One  point  of reference  is  the  Visual 
  296.         Calculater described in Chapter 14 of this  tutorial.   This 
  297.         program  is  contained in four separately compiled  programs 
  298.         and  is a rather large complex program.   If you spend  some 
  299.         time studying the source code,  you will find that there  is 
  300.         not  a single "goto" statement anywhere in it.   Compile and 
  301.         run  GOTOEX.C  and study its output.   It would  be  a  good 
  302.         exercise  to rewrite it and see how much more readable it is 
  303.         when the statements are listed in order.
  304.          
  305.                        FINALLY, A MEANINGFUL PROGRAM
  306.  
  307.              Load  the  file  named TEMPCONV.C for an example  of  a 
  308.         useful,  even  though somewhat limited program.   This is  a 
  309.         program  that generates a list of centigrade  and  farenheit 
  310.         temperatures  and prints a message out at the freezing point 
  311.         of water and another at the boiling point of water. 
  312.  
  313.              Of particular importance is the formatting.  The header 
  314.         is  simply  several lines of comments  describing  what  the 
  315.         program  does in a manner that catches the readers attention 
  316.         and  is  still  pleasing to the  eye.  You  will  eventually 
  317.         develop your own formatting style, but this is a good way to 
  318.         start.   Also if you observe the for loop,  you will  notice 
  319.  
  320.  
  321.                                  Page 16 
  322.  
  323.  
  324.  
  325.  
  326.  
  327.  
  328.  
  329.  
  330.  
  331.                         Chapter 3 - Program Control                        
  332.  
  333.  
  334.         that  all  of  the contents of the  compound  statement  are 
  335.         indented  3 spaces to the right of the "for" reserved  word, 
  336.         and  the  closing brace is lined up under the "f" in  "for".  
  337.         This  makes debugging a bit easier because the  construction 
  338.         becomes  very  obvious.   You  will  also  notice  that  the 
  339.         "printf"  statements that are in the "if" statements  within 
  340.         the  big  "for" loop are indented  three  additional  spaces 
  341.         because they are part of another construct. 
  342.  
  343.              This  is  the first program in which we used more  than 
  344.         one  variable.   The three variables are simply  defined  on 
  345.         three  different lines and are used in the same manner as  a 
  346.         single variable was used in previous programs.   By defining 
  347.         them  on different lines,  we have an opportunity to  define 
  348.         each with a comment.
  349.  
  350.                       ANOTHER POOR PROGRAMMING EXAMPLE
  351.  
  352.              Recalling  UGLYFORM.C from the last chapter,  you saw a 
  353.         very  poorly  formatted program.   If you load  and  display 
  354.         DUMBCONV.C you will have an example of poor formatting which 
  355.         is  much closer to what you will actually find in  practice.  
  356.         This  is  the same program as TEMPCONV.C with  the  comments 
  357.         removed  and  the  variable  names  changed  to  remove  the 
  358.         descriptive aspect of the names.  Although this program does 
  359.         exactly the same as the last one,  it is much more difficult 
  360.         to  read and understand.   You should begin to develop  good 
  361.         programming practices now.
  362.  
  363.              Compile  and  run  this program to  see  that  it  does 
  364.         exactly what the last one did.
  365.  
  366.  
  367.         PROGRAMMING EXERCISES
  368.  
  369.         1.  Write a program that writes your name on the monitor ten 
  370.             times.   Write this program three times, once with  each 
  371.             looping method.
  372.  
  373.         2.  Write a program that counts from one to ten, prints the 
  374.             values  on  a separate line for each,  and  includes  a     
  375.             message  of  your  choice when the count  is  3  and  a 
  376.             different message when the count is 7.
  377.  
  378.  
  379.  
  380.  
  381.  
  382.  
  383.  
  384.  
  385.  
  386.  
  387.                                  Page 17 
  388.  
  389.